package com.oracle.lingshangejia.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.adapter.CheckupHealthAdapter;
import com.oracle.lingshangejia.base.BaseActivity;
import com.oracle.lingshangejia.bean.CheckupHealthBean;
import com.oracle.lingshangejia.utils.MyLogger;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.OkHttpUtils;

import java.util.HashMap;

/*
    体检档案
 */
public class CheckupHealthActivity  extends BaseActivity implements View.OnClickListener {
    private XRecyclerView recyclerView;
    private TextView tv_title;
    private ImageButton ib_back;
    private ImageButton ib_right;
    private CheckupHealthAdapter adapter;
    private int jmid;
    private String tjbh;
    @Override
    public int intiLayout() {
        return R.layout.activity_checkuphealth;
    }

    @Override
    public void initView() {
        tv_title=findViewById(R.id.tv_title);
        tv_title.setText("体检档案");
        ib_back=findViewById(R.id.ib_back);
        ib_back.setOnClickListener(this);
        ib_right=findViewById(R.id.ib_list);
        ib_right.setVisibility(View.VISIBLE);
        ib_right.setImageDrawable(getResources().getDrawable(R.drawable.add_light));
        ib_right.setOnClickListener(this);
        recyclerView=findViewById(R.id.checkRecycle);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        jmid=getIntent().getIntExtra("jmid",0);
        tjbh=getIntent().getStringExtra("tjbh");
    }

    @Override
    public void initData() {
        //getNetWork();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getNetWork();
    }

    private void getNetWork() {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("jmid",jmid);
        OkHttpUtils.getNetWork(CheckupHealthActivity.this, Constant.baseurl+"online/tj/getTjrqList",map,handler);
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:  //成功S
                    String s= (String) msg.obj;
                    Gson gson=new Gson();
                    MyLogger.i(s);
                    try{
                        CheckupHealthBean bean=gson.fromJson(s, CheckupHealthBean.class);
                        if(bean.getCode()==0){
                            if (bean.getData().size()>0) {
                                adapter = new CheckupHealthAdapter(CheckupHealthActivity.this, bean.getData());
                                recyclerView.setAdapter(adapter);
                                adapter.setOnItemClickListener(new CheckupHealthAdapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(View view, int postion) {

                                    }
                                });
                            }else {
                                MyToast.show("没有数据");
                            }
                        }else{
                            Toast.makeText(CheckupHealthActivity.this,bean.getMsg(),Toast.LENGTH_SHORT).show();
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(CheckupHealthActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(CheckupHealthActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(CheckupHealthActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ib_back:
                finish();
                break;
            case R.id.ib_list:  //添加健康体检表
                Intent intent=new Intent(CheckupHealthActivity.this,HealthCheckArchivesActivity.class);
                intent.putExtra("jmid",jmid);
                intent.putExtra("tjbh",tjbh);//编号
                startActivity(intent);
                break;
        }
    }
}
